<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable width="80%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
			<el-form-item prop="username" label="用户名">
				<el-input v-model="dataForm.username" placeholder="用户名"></el-input>
			</el-form-item>
			<el-form-item prop="realName" label="姓名">
				<el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
			</el-form-item>
			<el-form-item prop="deptName" label="部门">
				<el-input v-model="dataForm.deptName" placeholder="部门" @click="addOrUpdateDeptHandle()"></el-input>
			</el-form-item>
			<!-- <el-form-item prop="orgId" label="所属机构">
				<el-tree-select
					v-model="dataForm.orgId"
					:data="orgList"
					value-key="id"
					check-strictly
					:render-after-expand="false"
					:props="{ label: 'name', children: 'children' }"
					style="width: 100%"
				/>
			</el-form-item> -->
			<el-form-item prop="gender" label="性别">
				<fast-radio-group v-model="dataForm.gender" dict-type="user_gender"></fast-radio-group>
			</el-form-item>
			<el-form-item prop="mobile" label="手机号">
				<el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
			</el-form-item>
			<el-form-item prop="email" label="邮箱">
				<el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
			</el-form-item>
			<el-form-item prop="password" label="密码">
				<el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
			</el-form-item>
			<el-form-item prop="regMoney" label="挂号费">
				<el-input v-model="dataForm.regMoney" type="number" placeholder="挂号费"></el-input>
			</el-form-item>
			<el-form-item prop="roleIdList" label="所属角色">
				<el-select v-model="dataForm.roleIdList" multiple placeholder="所属角色" style="width: 100%">
					<el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="postIdList" label="所属岗位">
				<el-select v-model="dataForm.postIdList" multiple placeholder="所属岗位" style="width: 100%">
					<el-option v-for="post in postList" :key="post.id" :label="post.postName" :value="post.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="status" label="状态">
				<fast-radio-group v-model="dataForm.status" dict-type="user_status"></fast-radio-group>
			</el-form-item>
			<el-form-item prop="userType" label="用户类型">
				<el-select v-model="dataForm.userType" style="width:100%;">
					<el-option
					v-for="item in dataForm.typelist"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item prop="userCategory" label="医生类型">
				<el-select v-model="dataForm.userCategory" style="width:100%;">
					<el-option
					v-for="item in dataForm.categoryList"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item prop="userDesc" label="用户简介">
				<el-input v-model="dataForm.userDesc" placeholder="简介" type="textarea"></el-input>
			</el-form-item>
			<el-form-item prop="userImg" label="头像">
				<el-upload
					:action="dataForm.uploadUrl"
					:on-success="handleUpAvatar"
					:on-remove="handleRemove"
					:limit="1"
            		:file-list="dataForm.fileList"
					list-type="picture-card"
					accept=".jpg,.jpeg,.png,.bmp"
				>
					<template #default>
					<el-icon><Plus /></el-icon>
					</template>
				</el-upload>
			</el-form-item>
			<div style="width:100%;height:300px;float:left; margin-top: 10px;margin-left: 10px;
			margin-bottom: 100px;">
					<WangEditor v-model="dataForm.remark"  placeholder="请输入描述..."></WangEditor>
			</div>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
		<!-- 弹窗, 新增 / 修改 -->
		<datalistUser ref="datalistUserRef" @fatherMethod="fatherMethod"></datalistUser>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrgListApi } from '@/api/sys/orgs'
import { useUserApi, useUserSubmitApi } from '@/api/sys/user'
import { usePostListApi } from '@/api/sys/post'
import { useRoleListApi } from '@/api/sys/role'
import WangEditor from '@/components/wang-editor/index.vue'
import datalistUser from './datalist.vue'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const postList = ref<any[]>([])
const roleList = ref<any[]>([])
const orgList = ref([])
const dataFormRef = ref()

const dataForm = reactive({
	uploadUrl: "http://zbzx.wicp.net:8070/sys/file/upload",
	id: '',
	username: '',
	realName: '',
	orgId: '',
	org:'',
	orgName: '',
	password: '',
	gender: 0,
	email: '',
	mobile: '',
	roleIdList: [] as any[],
	postIdList: [] as any[],
	status: 1,
	userDesc:'',
	userType:1,
	userImg:'',
	deptId:'',
	deptName:'',
	remark:'',
	userCategory:'',
	regMoney:0,
	typelist:[
		{
			value:1,
			label:'医生'
		},
		{
			value:2,
			label:'护士'
		},
		{
			value:3,
			label:'管理人员'
		}
	],
	categoryList:[
		{
			value:1,
			label:'普通'
		},
		{
			value:2,
			label:'专家'
		}
	],
	fileList:[]
})
const handleUpAvatar = (res:any) => {
	console.log(res);
	if(res.code==0){
		dataForm.userImg=res.data.url;
	}
};
//删除已上传图片
const handleRemove = (file:any, fileList:any) => {
	dataForm.userImg="";
	dataForm.fileList = [];
};
const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	let orgid:any=localStorage.getItem('orgId');
	dataForm.orgId=orgid;

	let org:any=localStorage.getItem('org');
	dataForm.org=org;
	// id 存在则为修改
	if (id) {
		getUser(id)
	}
	getPostList()
	getRoleList()
}

// 获取岗位列表
const getPostList = () => {
	var param={orgId:localStorage.getItem('orgId'),page:1,limit:1}
	return usePostListApi(param).then(res => {
		postList.value = res.data
	})
}

// 获取角色列表
const getRoleList = () => {
	var param={orgId:localStorage.getItem('orgId'),page:1,limit:1}
	return useRoleListApi(param).then(res => {
		roleList.value = res.data
	})
}


const datalistUserRef = ref()
const addOrUpdateDeptHandle = () => {
	datalistUserRef.value.init();
}
const fatherMethod=(orgTit:any,org:any)=>{
	dataForm.deptId=org;
	dataForm.deptName=orgTit;
}
// 获取信息
const getUser = (id: number) => {
	useUserApi(id).then(res => {
		let records:any = [{url: res.data.userImg}];
		dataForm.fileList=records;
		dataForm.remark=res.data.remark;
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	username: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	realName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	mobile: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	// orgId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useUserSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
